/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef PULSEAUDIOOUTPUT_H
#define PULSEAUDIOOUTPUT_H

#include "defines.h"

#include <QTimer>

#include <pulse/pulseaudio.h>

#include "soundengine.h"


class QBuffer;

class PulseAudioOutput : public QObject
{
    Q_OBJECT

#ifdef SMASHMINER_CONFIG_ENABLE_SOUND_ENGINE_PULSE_AUDIO

public:
    enum TimerPeriod { PeriodTimeMs = 100 };

    PulseAudioOutput(const QByteArray &device, QObject *parent = 0);
    ~PulseAudioOutput();

    void setVolumeLevel(int volumeLevel);
    void addSoundBuffer(SoundEffectType effectType, QBuffer *soundBuffer);
    void start(SoundEffectType soundEffect);
    void stop();
    void reset();
    void suspend();
    void resume();
    int bytesFree() const;
    QAudio::Error error() const;
    QAudio::State state() const;
    void setFormat(const QAudioFormat &format);
    QAudioFormat format() const;

private:
    bool startOutput();
    void stopOutput();
    qint64 write(const char *data, qint64 length);
    qint64 writeDataToOutputDevice();

private slots:
    void feedSoundData();

private:
    /// Audio sink for output.
    QByteArray device;
    /// Output stream name.
    QByteArray streamName;
    /// Audio Format for output.
    QAudioFormat audioFormat;
    /// Error state.
    QAudio::Error errorState;
    /// Device state.
    QAudio::State deviceState;
    /// Is the output stream opened.
    bool opened;
    /// Free bytes available at output stream.
    int bytesAvailable;
    /// Output stream
    pa_stream *stream;
    /// Size of buffer feed to output stream.
    int periodSizeValue;
    /// Timer for feeding data to output stream.
    QTimer *tickTimer;
    /// Is the playback resuming from suspend.
    bool resuming;
    /// Type of sound effect currently playing.
    SoundEffectType effectPlaying;
    /// Current output volume.
    int volume;
    /// Sound data array.
    QBuffer *soundsBufferArray[NumberOfSounds];

#endif // SMASHMINER_CONFIG_ENABLE_SOUND_ENGINE_PULSE_AUDIO

};

#endif // PULSEAUDIOOUTPUT_H
